package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.aliyuncs.CommonRequest;
import com.yxx.annotation.AccessLimit;
import com.yxx.annotation.OptLogger;
import com.yxx.entityModel.vo.Result.PageResult;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.query.CommentQuery;
import com.yxx.entityModel.vo.request.CheckRequest;
import com.yxx.entityModel.vo.request.CommentRequest;
import com.yxx.entityModel.vo.response.CommentBackResponse;
import com.yxx.entityModel.vo.response.CommentResponse;
import com.yxx.entityModel.vo.response.RecentCommentResponse;
import com.yxx.entityModel.vo.response.ReplyResponse;
import com.yxx.enums.LikeTypeEnum;
import com.yxx.service.CommentService;
import com.yxx.strategy.context.LikeStrategyContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static com.yxx.constant.OptTypeConstant.DELETE;
import static com.yxx.constant.OptTypeConstant.UPDATE;

/**
 * 重点
 * 评论控制器
 * @author yuxiangxun
 * @date 2025/2/6
 * @apiNote
 */
@RestController
@Api(tags = "评论接口模块")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private LikeStrategyContext likeStrategyContext;

    /**
     * 查看后台评论列表
     * @param commentQuery
     * @return
     */
    @ApiOperation(value = "查看后台评论")
    @SaCheckPermission("news:comment:list")
    @GetMapping("/admin/comment/list")
    public ResponseResult<PageResult<CommentBackResponse>> CommentBackResponseList(CommentQuery commentQuery){
        return ResponseResult.success(commentService.commentBackList(commentQuery));
    }

    /**
     * 添加评论
     * @param commonRequest
     * @return
     */
    @SaCheckLogin // 鉴权，登录校验，只有登录有才能进入该方法
    @SaCheckPermission("news:comment:add") // 权限校验， 必须具有指定权限才能进入该方法
    @ApiOperation(value = "添加评论")
    @PostMapping("/comment/add")
    public ResponseResult<?> addComment(@Validated @RequestBody CommentRequest commonRequest){
        commentService.addComment(commonRequest);
        return ResponseResult.success();
    }

    /**
     * 删除评论
     *
     * @param commentIdList 评论id
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = DELETE)
    @ApiOperation(value = "删除评论")
    @SaCheckPermission("news:comment:delete")
    @DeleteMapping("/admin/comment/delete")
    public ResponseResult<?> deleteComment(@RequestBody List<Integer> commentIdList) {
        commentService.removeByIds(commentIdList);
        return ResponseResult.success();
    }

    /**
     * 审核评论
     *
     * @param checkRequest 审核信息
     * @return {@link ResponseResult<>}
     */
    @OptLogger(value = UPDATE)
    @ApiOperation(value = "审核评论")
    @SaCheckPermission("news:comment:pass")
    @PutMapping("/admin/comment/pass")
    public ResponseResult<?> updateCommentCheck(@Validated @RequestBody CheckRequest checkRequest) {
        commentService.updateCommentCheck(checkRequest);
        return ResponseResult.success();
    }

    /**
     * 点赞评论
     * @param commentId
     * @return
     */
    @SaCheckLogin
    @SaCheckPermission("news:comment:like")
    @AccessLimit(seconds = 60, maxCount = 3)
    @ApiOperation(value = "点赞评论")
    @PostMapping("/comment/{commentId}/like")
    public ResponseResult<?> likeComment(@PathVariable("commentId") Integer commentId){
        likeStrategyContext.executeLikeStrategy(LikeTypeEnum.COMMENT,commentId);
        return ResponseResult.success();
    }


    /**
     * 查看最新评论
     *
     * @return {@link List <  RecentCommentResponse  >}
     */
    @ApiOperation(value = "查看最新评论")
    @GetMapping("/recent/comment")
    public ResponseResult<List<RecentCommentResponse>> listRecentCommentVO() {
        return ResponseResult.success(commentService.RecentCommentList());
    }

    /**
     * 重点
     * 查看评论
     * @param commentQuery 条件
     * @return {@link ResponseResult< CommentResponse >}
     */
    @ApiOperation(value = "查看评论")
    @GetMapping("/comment/list")
    public ResponseResult<PageResult<CommentResponse>> listCommentVO(CommentQuery commentQuery) {
        return ResponseResult.success(commentService.commentList(commentQuery));
    }

    /**
     * 查看回复评论
     * @param commentId 评论id
     * @return {@link ResponseResult< ReplyResponse >} 回复评论
     */
    @ApiOperation(value = "查看回复评论")
    @GetMapping("/comment/{commentId}/reply")
    public ResponseResult<List<ReplyResponse>> replyList(@PathVariable("commentId") Integer commentId){
        return ResponseResult.success(commentService.replyList(commentId));
    }
}
